<?php

namespace App\Http\Controllers\Mobile;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Bags;
use App\Models\Shudan;
use App\Models\BagsLiuyan;
use App\Models\UsersReg;
use App\Models\BooksComments; //短评
use App\Models\BooksReview; //长评
use App\Models\UsersAttrib;
use App\Models\BooksPingfen;
use App\Models\Chubaninfo; //出版社列表

use Session;
use Redirect;

class PinglunController extends Controller
{
    public function commentlist($bookid)
    {
        //显示发布的短书评
        $books = Book::find($bookid);
        $bookscomments = BooksComments::where('book_id', $bookid)->orderBy('add_time', 'desc')->get();        
        foreach($bookscomments as $comment)
        {
            //为了得到会员名字和头像
            $comment['users'] = $comment->hasOnUsersReg;
        }
        return view('mobile/comment', array(
            'bookscomments' => $bookscomments, 
            'books' => $books
            ));
    }

    public function reviewslist($bookid)
    {
        //显示长书评评论列表
        $books = Book::find($bookid);
        $booksreviews = BooksReview::where('book_id', $bookid)->orderBy('add_time', 'desc')->get();
        foreach ($booksreviews as $reviewuser) {
            //得到发布长评论的会员资料
            $reviewuser['users'] = $reviewuser->hasOnUsersReg;
        }

        return view('mobile.reviewslist', array(
            'booksreviews' => $booksreviews, 
            'books' => $books
        ));
    }

    public function commentpost($bookid)
    {
        //发表短书评
        $books = Book::find($bookid);
        return view('mobile/commentpost',array(
            'books' => $books
        ));
    }

    public function commentpost_save($bookid)
    {
        //保存发表的短书评
        $bookscomments = new BooksComments;

        $nameid = Session::get('nameid');

        $bookscomments->book_id = $bookid;                   //评论的图书的bookid
        $bookscomments->uid = $nameid;                   //评论的会员uid
        $bookscomments->message = request::input('message'); //发表的短评论内容
        $bookscomments->rating = 5;  //默认5分
        $bookscomments->add_time = time();                   //获取当前时间戳;

        $bookscomments->save();

        $users = UsersReg::find($nameid);
        $users->shubi = $users->shubi + 1; //增加一书币
        $users->save(); //更新书币

        return redirect('mobile/comment/'.$bookid);
    }

    public function reviewslistpost($bookid)
     {
        //发表长书评
        $books = Book::find($bookid);
        return view('mobile/reviewlistpost',array(
            'books' => $books
        ));
     }

     public function reviewslistpost_save($bookid)
     {
        //保存发表的长书评
        $booksreviews = new BooksReview;
        $booksreviews->book_id = $bookid;

        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid');
        $booksreviews->uid = $uid; // 保存书评用户的uid

        $booksreviews->title = trim(request::input('title'));
        //$booksreviews->rating = request::input('rating');
        $booksreviews->rating = 5; //默认5分
        $booksreviews->bookreview = request::input('bookreview');
        $booksreviews->add_time = time(); //获取当前时间戳

        $booksreviews->save();
        
        // $id = $booksreviews->id;
        // return Redirect('mobile/shupinginfo/'. $id); //跳转到评论内容页面

        return Redirect('mobile/reviewslist/' . $bookid); //跳转到评论发布列表页面
     }

}
